<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>soy.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_soy_soy.js.html">soy.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Provides utility methods to render soy template.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.soy&#39;);
<a name="line20"></a>
<a name="line21"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line22"></a>goog.require(&#39;goog.dom&#39;);
<a name="line23"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line24"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line25"></a>goog.require(&#39;goog.soy.data&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * @define {boolean} Whether to require all Soy templates to be &quot;strict html&quot;.
<a name="line30"></a> * Soy templates that use strict autoescaping forbid noAutoescape along with
<a name="line31"></a> * many dangerous directives, and return a runtime type SanitizedContent that
<a name="line32"></a> * marks them as safe.
<a name="line33"></a> *
<a name="line34"></a> * If this flag is enabled, Soy templates will fail to render if a template
<a name="line35"></a> * returns plain text -- indicating it is a non-strict template.
<a name="line36"></a> */
<a name="line37"></a>goog.soy.REQUIRE_STRICT_AUTOESCAPE = false;
<a name="line38"></a>
<a name="line39"></a>
<a name="line40"></a>/**
<a name="line41"></a> * Renders a Soy template and then set the output string as
<a name="line42"></a> * the innerHTML of an element. It is recommended to use this helper function
<a name="line43"></a> * instead of directly setting innerHTML in your hand-written code, so that it
<a name="line44"></a> * will be easier to audit the code for cross-site scripting vulnerabilities.
<a name="line45"></a> *
<a name="line46"></a> * @param {Element} element The element whose content we are rendering into.
<a name="line47"></a> * @param {Function} template The Soy template defining the element&#39;s content.
<a name="line48"></a> * @param {Object=} opt_templateData The data for the template.
<a name="line49"></a> * @param {Object=} opt_injectedData The injected data for the template.
<a name="line50"></a> */
<a name="line51"></a>goog.soy.renderElement = function(element, template, opt_templateData,
<a name="line52"></a>                                  opt_injectedData) {
<a name="line53"></a>  element.innerHTML = goog.soy.verifyTemplateOutputSafe_(template(
<a name="line54"></a>      opt_templateData || goog.soy.defaultTemplateData_, undefined,
<a name="line55"></a>      opt_injectedData));
<a name="line56"></a>};
<a name="line57"></a>
<a name="line58"></a>
<a name="line59"></a>/**
<a name="line60"></a> * Renders a Soy template into a single node or a document
<a name="line61"></a> * fragment. If the rendered HTML string represents a single node, then that
<a name="line62"></a> * node is returned (note that this is *not* a fragment, despite them name of
<a name="line63"></a> * the method). Otherwise a document fragment is returned containing the
<a name="line64"></a> * rendered nodes.
<a name="line65"></a> *
<a name="line66"></a> * @param {Function} template The Soy template defining the element&#39;s content.
<a name="line67"></a> * @param {Object=} opt_templateData The data for the template.
<a name="line68"></a> * @param {Object=} opt_injectedData The injected data for the template.
<a name="line69"></a> * @param {goog.dom.DomHelper=} opt_domHelper The DOM helper used to
<a name="line70"></a> *     create DOM nodes; defaults to {@code goog.dom.getDomHelper}.
<a name="line71"></a> * @return {!Node} The resulting node or document fragment.
<a name="line72"></a> */
<a name="line73"></a>goog.soy.renderAsFragment = function(template, opt_templateData,
<a name="line74"></a>                                     opt_injectedData, opt_domHelper) {
<a name="line75"></a>  var dom = opt_domHelper || goog.dom.getDomHelper();
<a name="line76"></a>  return dom.htmlToDocumentFragment(goog.soy.verifyTemplateOutputSafe_(
<a name="line77"></a>      template(opt_templateData || goog.soy.defaultTemplateData_,
<a name="line78"></a>               undefined, opt_injectedData)));
<a name="line79"></a>};
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * Renders a Soy template into a single node. If the rendered
<a name="line84"></a> * HTML string represents a single node, then that node is returned. Otherwise,
<a name="line85"></a> * a DIV element is returned containing the rendered nodes.
<a name="line86"></a> *
<a name="line87"></a> * @param {Function} template The Soy template defining the element&#39;s content.
<a name="line88"></a> * @param {Object=} opt_templateData The data for the template.
<a name="line89"></a> * @param {Object=} opt_injectedData The injected data for the template.
<a name="line90"></a> * @param {goog.dom.DomHelper=} opt_domHelper The DOM helper used to
<a name="line91"></a> *     create DOM nodes; defaults to {@code goog.dom.getDomHelper}.
<a name="line92"></a> * @return {!Element} Rendered template contents, wrapped in a parent DIV
<a name="line93"></a> *     element if necessary.
<a name="line94"></a> */
<a name="line95"></a>goog.soy.renderAsElement = function(template, opt_templateData,
<a name="line96"></a>                                    opt_injectedData, opt_domHelper) {
<a name="line97"></a>  var dom = opt_domHelper || goog.dom.getDomHelper();
<a name="line98"></a>  var wrapper = dom.createElement(goog.dom.TagName.DIV);
<a name="line99"></a>  wrapper.innerHTML = goog.soy.verifyTemplateOutputSafe_(template(
<a name="line100"></a>      opt_templateData || goog.soy.defaultTemplateData_,
<a name="line101"></a>      undefined, opt_injectedData));
<a name="line102"></a>
<a name="line103"></a>  // If the template renders as a single element, return it.
<a name="line104"></a>  if (wrapper.childNodes.length == 1) {
<a name="line105"></a>    var firstChild = wrapper.firstChild;
<a name="line106"></a>    if (firstChild.nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line107"></a>      return /** @type {!Element} */ (firstChild);
<a name="line108"></a>    }
<a name="line109"></a>  }
<a name="line110"></a>
<a name="line111"></a>  // Otherwise, return the wrapper DIV.
<a name="line112"></a>  return wrapper;
<a name="line113"></a>};
<a name="line114"></a>
<a name="line115"></a>
<a name="line116"></a>/**
<a name="line117"></a> * Verifies that a template result is &quot;safe&quot; to insert as HTML.
<a name="line118"></a> *
<a name="line119"></a> * Note if the template is non-strict autoescape, the guarantees here are very
<a name="line120"></a> * weak. It is recommended applications switch to requiring strict
<a name="line121"></a> * autoescaping over time.
<a name="line122"></a> *
<a name="line123"></a> * @param {*} templateResult The template result.
<a name="line124"></a> * @return {string} The assumed-safe HTML output string.
<a name="line125"></a> * @private
<a name="line126"></a> */
<a name="line127"></a>goog.soy.verifyTemplateOutputSafe_ = function(templateResult) {
<a name="line128"></a>  // Allow strings as long as strict autoescaping is not mandated. Note we
<a name="line129"></a>  // allow everything that isn&#39;t an object, because some non-escaping templates
<a name="line130"></a>  // end up returning non-strings if their only print statement is a
<a name="line131"></a>  // non-escaped argument, plus some unit tests spoof templates.
<a name="line132"></a>  // TODO(gboyer): Track down and fix these cases.
<a name="line133"></a>  if (!goog.soy.REQUIRE_STRICT_AUTOESCAPE &amp;&amp; !goog.isObject(templateResult)) {
<a name="line134"></a>    return String(templateResult);
<a name="line135"></a>  }
<a name="line136"></a>
<a name="line137"></a>  // Allow SanitizedContent of kind HTML.
<a name="line138"></a>  if (templateResult instanceof goog.soy.data.SanitizedContent) {
<a name="line139"></a>    templateResult = /** @type {!goog.soy.data.SanitizedContent} */ (
<a name="line140"></a>        templateResult);
<a name="line141"></a>    var ContentKind = goog.soy.data.SanitizedContentKind;
<a name="line142"></a>    if (templateResult.contentKind === ContentKind.HTML ||
<a name="line143"></a>        templateResult.contentKind === ContentKind.ATTRIBUTES) {
<a name="line144"></a>      return goog.asserts.assertString(templateResult.content);
<a name="line145"></a>    }
<a name="line146"></a>  }
<a name="line147"></a>
<a name="line148"></a>  goog.asserts.fail(&#39;Soy template output is unsafe for use as HTML: &#39; +
<a name="line149"></a>      templateResult);
<a name="line150"></a>
<a name="line151"></a>  // In production, return a safe string, rather than failing hard.
<a name="line152"></a>  return &#39;zSoyz&#39;;
<a name="line153"></a>};
<a name="line154"></a>
<a name="line155"></a>
<a name="line156"></a>/**
<a name="line157"></a> * Immutable object that is passed into templates that are rendered
<a name="line158"></a> * without any data.
<a name="line159"></a> * @type {Object}
<a name="line160"></a> * @private
<a name="line161"></a> */
<a name="line162"></a>goog.soy.defaultTemplateData_ = {};
</pre>


</body>
</html>
